home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12657 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news.rdc.puc-rio.br!usenet
  2. From: Paulo Eduardo Neves <neves@lmf-di.puc-rio.br>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Class constructor usage within another class constructor problem!
  5. Date: Tue, 19 Mar 1996 17:57:56 -0600
  6. Organization: Laboratorio de Metodos Formais - PUC-Rio
  7. Message-ID: <314F4A04.41C6@lmf-di.puc-rio.br>
  8. References: <DoEv3x.IDL@latcs1.lat.oz.au>
  9. NNTP-Posting-Host: wittgenstein.lmf-di.puc-rio.br
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; AIX 1)
  14. CC: boylesgj@lion.cs.latrobe.edu.au
  15.  
  16. Gregary J Boyles wrote:
  17. > See astericks for problems.
  18. >  // address.cpp
  19. >  #include "address.h"
  20. >  // Constructor
  21. >  Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  22. >  {
  23. >       Number=ANumber;
  24. >       // *******************************************************************
  25. >       // The next two lines create objects of class String. Do these objects
  26. >       // get placed into the Street and City members of class Address, i.e.
  27. >       // are these lines equivelent to Address.Street=AStreet etc or have I
  28. >       // got it wrong?
  29. >       // *******************************************************************
  30. >       String Street(AStreet);
  31. >       String City(ACity);
  32. >       Zip=AZip;
  33. >  }
  34.  
  35. When the code part of the starts it members were already created, so you
  36. can't call their constructors again. You MUST use a assign operation. 
  37.  
  38. The really best thing to do is to use the inicilizer list to instanciate
  39. your data members. This way :
  40.  
  41. Address::Address(int ANumber,const char *AStreet,
  42.                  const char *ACity,int AZip)
  43.         :Street(AStreet), City(ACity), Number(ANumber), Zip(AZip) {}
  44.  
  45. Working thid way you code is not just more elegant, but also more
  46. eficient. This happens because when using the first approach you call
  47. the default cnst, initialise it members with a defaut value, call the
  48. assign operator and assign it members with the desired value. Using the
  49. initialiser list you just call the copy cnst and already put the desired
  50. values in it members. Imagine if your objects contains objects that
  51. contains objects that ... how many desnecessary calls are you doing.
  52.  
  53. Another nice rule to follow is to always make your cnst inline when it
  54. has an empty body. This will ALWAYS make your programs faster.
  55.  
  56. hope this helps,
  57. -- 
  58. Paulo Eduardo Neves             Laboratorio de Metodos Formais - PUC-Rio
  59. mailto:neves@lmf-di.puc-rio.br  Tel.: (021)512-8045
  60.